/**  
* Title: MybatisConfig.java 
* Description:  
* Copyright: Copyright (c) 2017  
* Company: www.baidudu.com 
* @author xuchang  
* @date 2018年7月23日  
* @version 1.0  
*/  
package com.bclz.config;


import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;

import ch.qos.logback.classic.Logger;


/**  
* Title: MybatisConfig
* Description: sqlFactory配置
* @author xuchang 
* @date 2018年7月23日  
*/

@org.springframework.context.annotation.Configuration
@MapperScan(basePackages="com.bclz.mapper")
public class MybatisConfig {
	
	private Logger log=(Logger)LoggerFactory.getLogger(getClass());
	
	@Autowired
	private DruidDataSource ds;
	
	//配置mapper的扫描，找到所有的mapper.xml映射文件
    @Value("${mybatis.mapperLocations}")
    private String mapperLocations;
	
	@Bean
	public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
		SqlSessionFactoryBean factory=new SqlSessionFactoryBean();
		Configuration mpConfig=new Configuration();
		//关闭缓存
		mpConfig.setCacheEnabled(false);
		//采用preparestatement预处理，速度比较快
		mpConfig.setDefaultExecutorType(ExecutorType.REUSE);
		mpConfig.setUseGeneratedKeys(true);
		mpConfig.setMapUnderscoreToCamelCase(true);
		mpConfig.setJdbcTypeForNull(JdbcType.NULL);
		mpConfig.setDefaultStatementTimeout(600);
		//配置分页插件
		factory.setPlugins(new Interceptor[] {pageHelper()});
		Resource[] re=new PathMatchingResourcePatternResolver().getResources(mapperLocations);
		factory.setMapperLocations(re); ;
		factory.setDataSource(ds);
		factory.setVfs(SpringBootVFS.class);
		factory.setConfiguration(mpConfig);
		factory.setTypeAliasesPackage("com.bclz.entity;com.bclz.bo");
		return factory.getObject();
	}
	
	 @Bean
     public PageInterceptor pageHelper(){
         log.info("MyBatis分页插件PageHelper");
         //分页插件
         PageInterceptor pageHelper = new PageInterceptor();
         Properties properties = new Properties();
         //设置为true时，会将RowBounds第一个参数offset当成pageNum页码使用
         //和startPage中的pageNum效果一样
         properties.setProperty("offsetAsPageNum", "true");
         //设置为true时，使用RowBounds分页会进行count查询 
         properties.setProperty("rowBoundsWithCount", "true");
         //设置为true时，如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果
         properties.setProperty("pageSizeZero", "true");
         //启用合理化时，如果pageNum<1会查询第一页，如果pageNum>pages会查询最后一页
         //禁用合理化时，如果pageNum<1或pageNum>pages会返回空数据(默认)
         properties.setProperty("reasonable", "true");
         properties.setProperty("supportMethodsArguments", "true");
         //always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page 
         properties.setProperty("returnPageInfo", "check");
         properties.setProperty("params", "count=countSql");
         pageHelper.setProperties(properties);
         return pageHelper;
     }
	
}
